package com.ly.vcode;

import com.ly.vcode.bean.VcodeProperties;
import com.ly.vcode.web.VcodeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@AutoConfiguration
@ComponentScan("com.ly.vcode")
@ConditionalOnProperty(prefix = "vcode", name = "enable", matchIfMissing = true)
@EnableConfigurationProperties(VcodeProperties.class)
public class VcodeConfiguration implements WebMvcConfigurer {

    @Autowired
    VcodeProperties properties;

    @Autowired
    VcodeInterceptor vcodeInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (properties.getScope() != null && properties.getScope().isEmpty() == false) {
            registry.addInterceptor(vcodeInterceptor)
                    .addPathPatterns(properties.getScope());
        }
    }
}
